<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        table tr th,td{
            border: 1px solid black;
            text-align: center;
        }
        table{
            border: 1px;
            cell-padding:0;
            cell-spacing:0;
            width: 80%;
            margin-top: 20px;
        }
        table thead{
            background-color:#b3d7ff ;
        }
        a{
            cursor: pointer;
        }

    </style>
    <link rel="stylesheet" href="../css/pintuer.css">
    <link rel="stylesheet" href="../css/bootstrap.css">

    <script src="../js/jquery-3.4.1.min.js"></script>
    <script src="../js/popper.min.js"></script>
    <script src="../js/bootstrap-table1.15.3.js"></script>
    <script src="../js/bootstrap-table-zh-CN.min.js"></script>
    <script src="../js/bootstrap-min-4.3.1.js"></script>
    <script src="../js/pintuer.js"></script>
    <script src="../js/initMain.js"></script>
    <script type="text/javascript">
        function initTable(){
            $("#tableInfo").bootstrapTable({
                method:"post",
                contentType:"application/json",
                url:"/mDesignProcedure/queryMDesignProcedureModule.action",
                //设置交替行样式
                striped:true,
                //初始显示页数
                pageNum:1,
                //是否显示分页
                pagination:true,
                //设置是服务端分页还是客户端分页
                sidePagination:"server",
                //页大小
                pageSize:3,
                //分页列表
                pageList: [3,5,6,10],
                //上传到服务器的参数
                queryParams:function (data) {
                    return{
                        //计算页号，当前号
                        pageNum:(data.offset/data.limit)+1,
                        //计算页大小
                        pageSize: data.limit,
                    }
                },
                columns:[
                    {title:"工序设计单编号",field:"designId",align:"center"},
                    {title:"产品编号",field:"productId",align:"center"},
                    {title:"产品名称",field:"productName",align:"center"},
                    {title:"设计人",field:"designer",align:"center"},
                    {title:"登记时间",field:"registerTime",align:"center"},
                    {title:"工时总成本",field:"costPriceSum",align:"center"},
                    {title:"制定",field:"id",align:"center",formatter:function (value,row,index) {
                            var btn="<a href='#' onclick='addModule("+JSON.stringify(row)+")'>制定</a>";
                            return btn;
                        }},
                ]

            })
        }
        var mDesignProcedureDetailId;
        var mDesignProcedureDetail;
        $(function () {

            initTable();

            //点击设计按钮
            $(document).on("click",".designModule",function () {
                $("#moduleSubtotal").text("0.00");
                if($(this).text()=="已设计"){
                    if(confirm("重新设计数据将会清空，你确定要重新设计吗？")){
                        mDesignProcedureDetail=$(this);
                        mDesignProcedureDetailId=$(this).attr("id");
                        $("#readBtn").css("display","block");
                        $("#addModelBtn").css("display","none");
                        // alert(id);
                        $.ajax({
                            url:"/mDesignProcedureDetails/queryAndUpdateMDesignProcedureDetailsById.action",
                            data: "mDesignProcedureDetailId="+mDesignProcedureDetailId+"&mDesignProcedureId="+mDesignProcedure.id+"&productId="+mDesignProcedure.productId,
                            type: "post",
                            success:function (data) {
                                // alert(data);
                                if(data=="null" || data.length==0 ||data==''){
                                    alert("请先完成产品物料组成设计审核！");
                                }else{
                                    $("#addDesignModel").modal("show");
                                    $("#procedureId").text(data.procedureId);
                                    $("#procedureName").text(data.procedureName);
                                    $("#register").text(localStorage.getItem("userName"));
                                    var date=new Date();
                                    $("#registerTime").text(date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+"  "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds());
                                    var desidualAmount=[];
                                    //重新设计时将删除该工序对应的物料信息，并更新物料的可用数量
                                    //查询产品对应的物料，并把数据插入到表格中
                                    $.ajax({
                                        url:"/dModuleDetails/queryDModuleDetailsByProductId.action",
                                        data:"productId="+mDesignProcedure.productId,
                                        type:"post",
                                        success:function (data) {
                                            // alert(data);
                                            $("#dModuleDetailsTBody").empty();
                                            for(var i=0;i<data.length;i++){
                                                var dModuleDetails=data[i];


                                                var trnew=$("<tr>");
                                                var td=$("<td>");
                                                td.html(dModuleDetails.id);
                                                var td1=$("<td>");
                                                td1.text(dModuleDetails.productName);
                                                var td2=$("<td>");
                                                td2.text(dModuleDetails.productId);
                                                var td3=$("<td>");
                                                td3.text(dModuleDetails.amount);
                                                var td4=$("<td class='desidualAmount'>");
                                                td4.text(dModuleDetails.desidualAmount);
                                                var td5=$("<td>");
                                                td5.text(dModuleDetails.amountUnit);
                                                var td6=$("<td class='costPrice'>");
                                                td6.text(dModuleDetails.costPrice);
                                                var td7=$("<td>");
                                                td7.html("<input class='amount' style='width: 100px;border: none'/>");
                                                var td8=$("<td>");
                                                td8.text("0.00");

                                                trnew.append(td);
                                                trnew.append(td1);
                                                trnew.append(td2);
                                                trnew.append(td3);
                                                trnew.append(td4);
                                                trnew.append(td5);
                                                trnew.append(td6);
                                                trnew.append(td7);
                                                trnew.append(td8);

                                                $("#dModuleDetailsTBody").append(trnew);

                                            }
                                        }
                                    })
                                }
                            }
                        })
                    }
                }else{
                    mDesignProcedureDetail=$(this);
                    mDesignProcedureDetailId=$(this).attr("id");
                    $("#readBtn").css("display","block");
                    $("#addModelBtn").css("display","none");

                    // alert(id);
                    $.ajax({
                        url:"/mDesignProcedureDetails/queryMDesignProcedureDetailsById.action",
                        data: "mDesignProcedureDetailId="+mDesignProcedureDetailId+"&mDesignProcedureId="+mDesignProcedure.id+"&productId="+mDesignProcedure.productId,
                        type: "post",
                        success:function (data) {
                            // alert(data);
                            if(data=="null" || data.length==0 ||data==''){
                                alert("请先完成产品物料组成设计审核！");
                            }else{
                                $("#addDesignModel").modal("show");
                                $("#procedureId").text(data.procedureId);
                                $("#procedureName").text(data.procedureName);
                                $("#register").text(localStorage.getItem("userName"));
                                var date=new Date();
                                $("#registerTime").text(date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+"  "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds());
                                var desidualAmount=[];
                                //查询产品对应的物料，并把数据插入到表格中
                                $.ajax({
                                    url:"/dModuleDetails/queryDModuleDetailsByProductId.action",
                                    data:"productId="+mDesignProcedure.productId,
                                    type:"post",
                                    success:function (data) {
                                        // alert(data);
                                        $("#dModuleDetailsTBody").empty();
                                        for(var i=0;i<data.length;i++){
                                            var dModuleDetails=data[i];


                                            var trnew=$("<tr>");
                                            var td=$("<td>");
                                            td.html(dModuleDetails.id);
                                            var td1=$("<td>");
                                            td1.text(dModuleDetails.productName);
                                            var td2=$("<td>");
                                            td2.text(dModuleDetails.productId);
                                            var td3=$("<td>");
                                            td3.text(dModuleDetails.amount);
                                            var td4=$("<td class='desidualAmount'>");
                                            td4.text(dModuleDetails.desidualAmount);
                                            var td5=$("<td>");
                                            td5.text(dModuleDetails.amountUnit);
                                            var td6=$("<td class='costPrice'>");
                                            td6.text(dModuleDetails.costPrice);
                                            var td7=$("<td>");
                                            td7.html("<input class='amount' style='width: 100px;border: none'/>");
                                            var td8=$("<td>");
                                            td8.text("0.00");

                                            trnew.append(td);
                                            trnew.append(td1);
                                            trnew.append(td2);
                                            trnew.append(td3);
                                            trnew.append(td4);
                                            trnew.append(td5);
                                            trnew.append(td6);
                                            trnew.append(td7);
                                            trnew.append(td8);

                                            $("#dModuleDetailsTBody").append(trnew);

                                        }
                                    }
                                })
                            }
                        }
                    })
                }


            })
            //预览
            $("#readBtn").click(function () {
                //判断表格的行是否大于一
                if($("#dModuleDetailsTBody tr").length>0){
                    var b=true;

                    var moduleSubtotal=0;
                    for(var i=0;i<$("#dModuleDetailsTBody tr").length;i++){
                        //个数
                        var amount=$(".amount").eq(i).val();
                        //可用数量
                        var desidualAmount=$(".desidualAmount").eq(i).text();

                        if(amount>desidualAmount){
                            alert("本工序数量不能大于可用数量！");
                            b=false;
                            break;
                        }else{
                            //单价
                            var costPrice=$(".costPrice").eq(i).text();
                            $("#dModuleDetailsTBody tr").eq(i).children("td").eq(8).text(amount*costPrice);

                            moduleSubtotal+=amount*costPrice;
                        }

                    }
                    $("#moduleSubtotal").text(moduleSubtotal);
                }else {
                    alert("请至少添加一个工序！");
                }
                if(b==true){
                    $(this).css("display","none");
                    $("#addModelBtn").css("display","block");
                }

            })
        })
        //点击提交按钮
        $(document).on("click","#saveModule",function () {
            // alert(mDesignProcedure.id);
            mDesignProcedure.moduleCostPriceSum=$("#moduleCostPriceSum").text();
            $.ajax({
                data:"mDesignProcedureStr="+JSON.stringify(mDesignProcedure),
                type:"post",
                url:"/mDesignProcedure/saveModule.action",
                success:function (data) {
                    $("#addModel").modal("hide");
                    $("#tableInfo").bootstrapTable("refresh");
                }
            })
        })
        $(document).on("click","#addModelBtn",function () {
            var dModuleDetailsArrays=[];
            var mDesignProcedureModuleArrays=[];

            for(var i=0;i<$("#dModuleDetailsTBody tr").length;i++){
                var tr=$("#dModuleDetailsTBody tr").get(i);

                //序号
                var id=tr.children[0].innerText;
                //物料名称
                var productName=tr.children[1].innerText;
                //物料编号
                var productId=tr.children[2].innerText;
                //设计数量
                var amount=tr.children[3].innerText;
                //可用数量
                var desidualAmount=tr.children[4].innerText;
                //单位
                var amountUnit=tr.children[5].innerText;
                //单价
                var costPrice=tr.children[6].innerText;
                //本工序数量
                var userAmount=$(".amount").eq(i).val();
                //小计
                var subtotal=tr.children[8].innerText;





                var dModuleDetails=new Object();
                //重新计算可用数量
                dModuleDetails.desidualAmount=desidualAmount-userAmount;
                dModuleDetails.id=id;
                dModuleDetailsArrays.push(dModuleDetails);
                // alert(dModuleDetails.desidualAmount);

                var mDesignProcedureModule=new Object();
                mDesignProcedureModule.parentId=mDesignProcedureDetailId;
                mDesignProcedureModule.detailsNumber=i+1;
                // alert(mDesignProcedureModule.detailsNumber);

                mDesignProcedureModule.productName=productName;
                mDesignProcedureModule.productId=productId;
                mDesignProcedureModule.amount=userAmount;
                mDesignProcedureModule.amountUnit=amountUnit;
                mDesignProcedureModule.costPrice=costPrice;
                mDesignProcedureModule.subtotal=subtotal;
                mDesignProcedureModuleArrays.push(mDesignProcedureModule);

            }
            mDesignProcedureDetail.moduleSubtotal=$("#moduleSubtotal").text();
            mDesignProcedureDetail.id=mDesignProcedureDetailId;
            // alert(mDesignProcedureDetail.id);

            $.ajax({
                url:"/mDesignProcedureModule/addMDesignProcedureModule.action",
                data :
                    "dModuleDetailsArrays="+JSON.stringify(dModuleDetailsArrays)+"&mDesignProcedureModuleArrays="+JSON.stringify(mDesignProcedureModuleArrays)
                    +"&mDesignProcedureDetailStr="+JSON.stringify(mDesignProcedureDetail)+"&mDesignProcedureDetailId="+mDesignProcedureDetailId,
                type:"post",
                success:function (data) {
                    mDesignProcedureDetail.text("已设计");
                    mDesignProcedureDetail.parent().prev().text($("#moduleSubtotal").text());
                    var moduleCostPriceSum=0;
                    for(var i=0;i<$("#tBody tr").length;i++){
                        moduleCostPriceSum+=parseFloat($(".moduleSubtotal").eq(i).text());
                    }
                    $("#moduleCostPriceSum").text(moduleCostPriceSum);
                    $("#addDesignModel").modal("hide");
                }

            })
        })
        var mDesignProcedure;
        //点击制定按钮
        function  addModule(obj) {
            $.ajax({
                url:"/mDesignProcedureDetails/queryMDesignProcedureDetailsByPId",
                type:"post",
                data:"pId="+obj.id,
                success:function (data) {
                    if(data.length==0||data==""||data=="null"){
                        alert("请先完成产品生产工序设计单审核！");
                    }else{
                        mDesignProcedure=obj;
                        mDesignProcedure.id=obj.id;

                        $("#addModel").modal("show");
                        $("#tBody").empty();
                        $(".aa").attr("disabled",false);
                        $("#productId").text(obj.PRODUCT_ID);
                        $("#productName").text(obj.PRODUCT_NAME);
                        //登记人：当前登录用户
                        // alert(localStorage.getItem("userName"));
                        $("#designer").text(obj.designer);
                        $("#designId").text(obj.designId);
                        $("#costPriceSum").text(obj.costPriceSum);
                        $("#procedureDescribe").text(obj.procedureDescribe);
                        $("#procedureChecker").text(obj.procedureChecker);
                        $("#procedureCheckTime").text(obj.procedureCheckTime);


                        for(var i=0;i<data.length;i++){
                            var mDesignProcedureDetail=data[i];
                            var trnew=$("<tr>");
                            var td=$("<td class='id'>");
                            td.html(mDesignProcedureDetail.id);
                            // count++;
                            var td1=$("<td>");
                            td1.text(mDesignProcedureDetail.procedureName);
                            var td2=$("<td>");
                            td2.text(mDesignProcedureDetail.procedureId);
                            //工时数
                            var td3=$("<td>");
                            if(mDesignProcedureDetail.amountUnit=='hour'){
                                td3.html(mDesignProcedureDetail.labourHourAmount);
                            }else if(mDesignProcedureDetail.amountUnit=='minute'){
                                td3.html(mDesignProcedureDetail.labourHourAmount/60.0);
                            }else if(mDesignProcedureDetail.amountUnit=='day'){
                                td3.html(mDesignProcedureDetail.labourHourAmount*24);
                            }
                            var td4=$("<td>");
                            td4.html(mDesignProcedureDetail.subtotal);

                            var td5=$("<td class='moduleSubtotal'>");

                            // td5.html("<input  style='width: 100px;border: none' class='amountUnit'/>");
                            td5.html(mDesignProcedureDetail.moduleSubtotal);

                            var td6=$("<td>");
                            var a=$("<a>");
                            a.attr("id",mDesignProcedureDetail.id)
                                .attr("class","designModule");
                            a.css("color","#005cbf");
                            // td6.html("<a id='"+mDesignProcedureDetail.id+"' class='designModule' style='color: #005cbf;'></a>");

                            if(mDesignProcedureDetail.designModuleTag=="D002-1"){
                                a.text("已设计");
                            }else{
                                a.text("设计");
                            }
                            td6.append(a);


                            trnew.append(td);
                            trnew.append(td1);
                            trnew.append(td2);
                            trnew.append(td3);
                            trnew.append(td4);
                            trnew.append(td5);
                            trnew.append(td6);
                            $("#tBody").append(trnew);


                        }
                        var moduleCostPriceSum=0;
                        for(var i=0;i<$("#tBody tr").length;i++){
                            moduleCostPriceSum+=parseFloat($(".moduleSubtotal").eq(i).text());
                        }
                        $("#moduleCostPriceSum").text(moduleCostPriceSum);
                    }

                }
            })
        }

    </script>
</head>
<body>
    <div>
        <table id="tableInfo">

        </table>
    </div>
    <div class="modal  fade" id="addModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="width: 90%">
            <div class="modal-content" style="border: 1px solid black;">
                <div class="modal-header">
                    <div class="container">
                        <div class="row">
                            <div class="col-7" style="text-align: right">
                                <h5 class="modal-title" id="info" >
                                    工序物料设计单
                                </h5>
                            </div>
                            <div class="col-3"></div>
                            <div class="col-2" style="position: absolute;right: 30px">

                                <button class="btn btn-primary" id="saveModule" >提交</button>
                            </div>
                        </div>
                    </div>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="margin-right: 0px">

                    </button>
                </div>
                <div class="modal-body">
                    <form id="addProcedureForm">
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-2" style="text-align: right">
                                    <span>工序单编号：</span>
                                </div>
                                <div class="col-4">
                                    <span id="designId"></span><br>
                                </div>
                                <div class="col-2" style="text-align: right">
                                    <span>设计人：</span>
                                </div>
                                <div class="col-4">
                                    <span id="designer"></span><br>
                                </div>

                            </div>
                        </div><br>

                        <div class="container">
                            <div class="row">
                                <div class="col-2" style="text-align:right">
                                    产品名称：
                                </div>
                                <div class="col-4">
                                    <span id="productName">笔记本</span>
                                </div>
                                <div class="col-2" style="text-align:right">
                                    产品编号：
                                </div>
                                <div class="col-4">
                                    <span id="productId" >20001002192001</span>
                                </div>
                            </div>
                        </div><br>


                        <table width="800px" style="margin-left: 20px">
                            <thead>
                                <tr>
                                    <th>序号</th>
                                    <th>工序名称</th>
                                    <th>工序编号</th>
                                    <th>工时数(小时)</th>
                                    <th>工时成本小计(元)</th>
                                    <th>物料成本小计(元)</th>
                                    <th>设计</th>
                                </tr>
                            </thead>
                            <tbody id="tBody">

                            </tbody>
                        </table><br>
                        <div class="container">
                            <div class="row">
                                <div class="col-2" style="text-align:right">
                                    工时总成本：
                                </div>
                                <div class="col-4">
                                    <span name="costPriceSum" id="costPriceSum"></span>
                                </div>
                                <div class="col-2" style="text-align:right">
                                    物料总成本：
                                </div>
                                <div class="col-4">
                                    <span id="moduleCostPriceSum">0.00</span>
                                </div>
                            </div>
                        </div><br>
                        <div class="container">
                            <div class="row">
                                <div class="col-2" style="text-align:right">
                                    审核人：
                                </div>
                                <div class="col-4">
                                    <span name="procedureChecker" id="procedureChecker"></span>
                                </div>
                                <div class="col-2" style="text-align:right">
                                    审核时间：
                                </div>
                                <div class="col-4">
                                    <span  name="procedureCheckTime" id="procedureCheckTime"></span>
                                </div>
                            </div>
                        </div><br>
                        <span style="margin-left: 55px">设计要求：</span>
                        <span id="procedureDescribe">

                        </span>

                    </form>
                </div>

            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>


<!--    设计工序物料-->
    <div class="modal  fade" id="addDesignModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog" style="width: 90%;height: 500px">
            <div class="modal-content" style="border: 1px solid black;">
                <div class="modal-header">
                    <button class="btn btn-primary" id="addModelBtn" style="position: absolute;right: 90px;display: none">提交</button>
                    <button class="btn btn-primary" id="readBtn" style="position: absolute;right: 90px">预览</button>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="margin-right: 0px">

                    </button>
                </div>
                <div style="width: 80%;height: 400px;border: 2px solid gray;margin: 0 auto;margin-top: 10px">
                    <h3 align="center" style="margin-top: 30px">工序物料设计单</h3>
                    <div class="container-fluid">
                        <form id="putRegister">
                            <div class="row" style="margin-top: 30px;">
                                <div class="col-3" style="text-align:right">
                                    工序单编号：
                                </div>
                                <div class="col-4">
                                    <span id="procedureId"></span>
                                </div>
                                <div class="col-2" style="text-align:right">
                                    工序名称：
                                </div>
                                <div class="col-3">
                                    <span id="procedureName"></span>
                                </div>

                            </div>
                            <div class="row" style="margin-top: 30px;">
                                <div class="col-3" style="text-align:right">
                                    登记人：
                                </div>
                                <div class="col-4">
                                    <span id="register"></span>
                                </div>
                                <div class="col-2" style="text-align:right">
                                    登记时间：
                                </div>
                                <div class="col-3">
                                    <span id="registerTime"></span>
                                </div>

                            </div>


                            <div>
                                <table width="80%" style="margin-top: 20px;" class="table table-sm">
                                    <thead id="tHead" style="background-color: #b3d7ff;">
                                    <tr >
                                        <!--<th>序号</th>
                                        <th>物料名称</th>
                                        <th>物料编号</th>
                                        <th>本工序数量</th>
                                        <th>单位</th>
                                        <th>单价(元)</th>
                                        <th>小计(元)</th>-->
                                        <th>序号</th>
                                        <th>物料名称</th>
                                        <th>物料编号</th>
                                        <th>设计数量</th>
                                        <th>可用数量</th>
                                        <th>单位</th>
                                        <th>单价(元)</th>
                                        <th>本工序数量</th>
                                        <th>小计(元)</th>
                                    </tr>
                                    </thead>
                                    <tbody id="dModuleDetailsTBody">

                                    </tbody>
                                </table>
                            </div>
                            <div class="row" style="margin-top: 30px;">
                                <div class="col-3" style="text-align:right">
                                    物料总成本：
                                </div>
                                <div class="col-4">
                                    <span id="moduleSubtotal"></span>
                                </div>

                            </div>

                        </form>
                    </div>
                </div>

            </div>
        </div>
    </div>
</body>

</html>